import { createRouter, createWebHistory } from 'vue-router';
import useBatchStore from '@/store/batchStore';

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/login',
      name: 'Login',
      component: () => import('@/views/Login/index.vue'),
      meta: {
        title: '登录',
      },
    },
    {
      path: '/',
      name: 'home',
      component: () => import('@/views/Home/index.vue'),
      redirect: '/welcome',
      children: [
        {
          path: '/welcome',
          name: 'welcome',
          component: () => import('@/views/HomeView.vue'),
          meta: {
            title: '首页',
          },
        },
        {
          path: '/acountManage',
          name: 'AcountManage',
          component: () => import('@/views/Account/index.vue'),
          meta: {
            title: '账号管理',
          },
        },
        {
          path: '/examManage',
          name: 'ExamManage',
          component: () => import('@/views/ExamNo/index.vue'),
          meta: {
            title: '考号管理',
          },
        },
        {
          path: '/vipManage',
          name: 'VipManage',
          component: () => import('@/views/Vip/index.vue'),
          meta: {
            title: 'VIP管理',
          },
        },
        {
          path: '/batchManage',
          name: 'BatchManage',
          component: () => import('@/views/Batch/index.vue'),
          meta: {
            title: '考试管理',
          },
        },
        {
          path: '/goodsManage',
          name: 'GoodsManage',
          component: () => import('@/views/Goods/index.vue'),
          meta: {
            title: '商品管理',
          },
        },
        {
          path: '/orderManage',
          name: 'OrderManage',
          component: () => import('@/views/Order/index.vue'),
          meta: {
            title: '订单管理',
          },
        },
        {
          path: '/messageManage',
          name: 'MessageManage',
          component: () => import('@/views/Message/index.vue'),
          meta: {
            title: '留言管理',
          },
        },
        {
          path: '/cacheManage',
          name: 'cacheManage',
          component: () => import('@/views/Cache/index.vue'),
          meta: {
            title: '缓存管理',
          },
        },
        {
          path: '/adManage',
          name: 'adManage',
          component: () => import('@/views/AdManage/index.vue'),
          meta: {
            title: '广告管理',
          },
        },
      ],
    },
  ],
});

router.beforeEach((to, from, next) => {
  if (to.path == '/login') {
    next();
  } else {
    if (window.sessionStorage.uid) {
      /* 路由发生变化修改页面title */
      if (to.meta.title) {
        useBatchStore().systemName = to.meta.title;
      }
      next();
    } else {
      next('/login');
    }
  }
});

export default router;
